﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;

using Microsoft.WowAddonStudio.FrameXml.Formats.Blp;
using Microsoft.WowAddonStudio.FrameXml.Formats.Tga;

namespace Microsoft.WowAddonStudio.FrameXml.Services
{
    [Guid("A8CAC87B-392A-4263-8C08-FDD42C50A2D0")]
    public class FrameXmlImageService : FrameXmlResourceService
    {
        private readonly Dictionary<string, Image> loadedImages = new Dictionary<string, Image>();

        /// <summary>
        /// Initializes a new instance of the <see cref="FrameXmlImageService"/> class.
        /// </summary>
        /// <param name="resourceFileLoader">The resource file loader.</param>
        public FrameXmlImageService(ResourceFileLoader resourceFileLoader)
            : base(resourceFileLoader)
        {
        }

        /// <summary>
        /// Checks whether an image exists.
        /// </summary>
        /// <param name="imagePath">The path to the image.</param>
        /// <returns>True, if the image exists; False otherwise.</returns>
		public virtual bool ImageExists(string imagePath)
        {
			if(String.IsNullOrEmpty(imagePath))
				return false;

            string extension = Path.GetExtension(imagePath).ToLower();
            if (String.IsNullOrEmpty(extension))
            {
                extension = ".blp";
                imagePath += extension;
            }

            // Check if image has been already loaded
            if (loadedImages.ContainsKey(imagePath))
                return true;

            return ResourceFileLoader.ResourceFileExists(imagePath);
        }

        /// <summary>
        /// Gets an image resource.
        /// </summary>
        /// <param name="imagePath">The path to the image.</param>
        /// <returns>An instance of the <see cref="Image"/> class, if the image was found; null otherwise.</returns>
		public virtual Image GetImage(string imagePath)
        {
            if (disposed)
                throw new ObjectDisposedException("MpqFontCollection");
            if (imagePath == null)
                throw new ArgumentNullException("imagePath");

            string extension = Path.GetExtension(imagePath).ToLower();
            if (String.IsNullOrEmpty(extension))
            {
                extension = ".blp";
                imagePath += extension;
            }

            // Check if image has been already loaded
            if (loadedImages.ContainsKey(imagePath))
                return loadedImages[imagePath];

			if (extension != ".blp" && extension != ".tga")
                return null;

            using (Stream imageStream = ResourceFileLoader.GetResourceFileStream(imagePath))
            {
                if (imageStream == null)
                    return null;

                Image image = null;

                switch (extension)
                {
                    case ".blp":
                        image = BlpImage.FromStream(imageStream);
                        break;
                    case ".tga":
                        image = TargaImage.FromStream(imageStream);
                        break;
                }

                if (image != null)
                {
                    // Add image to dictionary of loaded images
                    loadedImages.Add(imagePath, image);
                }

                return image;
            }
        }
    }
}
